#include "stdafx.h"
#include "Ball.h"

Ball::Ball(int gWidth,int gHeight){
	xPos = gWidth/2;
	yPos = gHeight/2;
	width = gWidth;
	height = gHeight;
	angle = 135;
	speed = 10;
	radius = 8;
}

void Ball::draw(IplImage *image){
	cvDrawCircle(image, cvPoint(xPos, yPos), radius, CV_RGB(255,255,255),-1);
}

void Ball::updateBall(int x1, int y1, int x2, int y2, int racketHeight){
	int center = radius / 2;
    if (xPos + center >= width)
    {
       bounce(true);
    }
    if (xPos - center <= 1)
    {
       bounce(true);
    }
    if (yPos + center >= height)
       bounce(false);
    if (yPos - center <= 1)
       bounce(false);
	

    xPos += (cos(M_PI * angle / 180) * speed);
    yPos -= (sin(M_PI * angle / 180) * speed);
}

void Ball::bounce(bool vertical){
	if (vertical)
		if (angle > 0)
			angle = 180 - angle;
        else
            angle = -(angle + 180);
        else //Horizontal
            angle = -angle;

        if(rand()%2)
            angle += rand()%3;
        else
            angle -= rand()%3;
}

unsigned int Ball::getX(){
	return xPos;
}

unsigned int Ball::getY(){
	return yPos;
}

unsigned int Ball::getRadius(){
	return yPos;
}
